﻿using System;
using EnvDTE80;
using EnvDTE;
using Microsoft.VisualStudio.CommandBars;
using stdole;
using System.Text;
using System.Diagnostics;

namespace Projky.Common {
    class ScriptInitializer {
        SsmsAbstract _ssmsAbstract;

        internal void Initialize(SsmsAbstract ssmsAbstract) {
            _ssmsAbstract = ssmsAbstract;
            AddScriptFoldersCommand(ssmsAbstract);
        }

        void AddScriptFoldersCommand(SsmsAbstract ssmsAbstract) {
            ScriptFolder[] folders = ScriptIOHelper.GetFolders();

            foreach (var folder in folders) {
                try {
                    var folderControl = _ssmsAbstract.command_manager.create_toolbar_menu(
                    "MenuBar",
                    folder.BarName,
                    folder.DirectoryPath,
                    0);

                    _ssmsAbstract.command_manager.SuppressCommandBar(folderControl);

                    try {
                        folderControl.Position = MsoBarPosition.msoBarTop;
                        folderControl.Left = 300;
                    } catch (Exception) { } // ignored

                    AddScriptFolderCommand(folder, folderControl);
                } catch (Exception ex) {
                    Debug.WriteLine(ex.Message);
                }
            }

            AddReloadProjkyAddinCommand();
        }

        void AddReloadProjkyAddinCommand() {
            _ssmsAbstract.command_manager.create_bar_command(
                        "Tools",
                        "ProjkyReloadAdddin",
                        "Reload ProjkyAddin",
                        "Reload all script in ProjkyAddin.",
                        5,
                        null,
                        create_reload_projkyaddin_exec_handler(),
                        null,
                        true);
        }

        void AddScriptFolderCommand(ScriptFolder folder, CommandBar toptool) {
            if (folder == null || toptool == null)
                return;

            foreach (CommandBarControl cbc in toptool.Controls) {
                cbc.Delete();
            }

            foreach (var script in folder.GetScripts()) {

                try {
                    _ssmsAbstract.command_manager.create_bar_command(
                        folder.BarName,
                        script.CommandName,
                        script.DispalyName,
                        script.Content,
                        0,
                        null,
                        create_openscript_exec_handler(script.Content),
                        ScriptIOHelper.GetScriptBitmap(),
                        false);
                } catch { }
            }
        }

        menu_command_exec_handler create_openscript_exec_handler(string scriptContent) {
            menu_command_exec_handler exec_handler = delegate(vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) {
                query_window query = _ssmsAbstract.window_manager.active_query_window;

                if (query == null)
                    query = _ssmsAbstract.window_manager.create_query_window();

                string content = scriptContent;
                query.insert_in_current(content);
            };

            return exec_handler;
        }

        menu_command_exec_handler create_reload_projkyaddin_exec_handler() {
            menu_command_exec_handler exec_handler = delegate(vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) {
                _ssmsAbstract.command_manager.CustomCleanup();

                AddScriptFoldersCommand(_ssmsAbstract);
            };

            return exec_handler;
        }
    }
}